//VERSION 1.0 - Flown on Mini Bexus 26. Sept 2012 to ~10km alt
//returns relative stick position from -100 to 100 for roll
int vx_g_rcin_roll() 
{
  long vx_rcin_roll = ((int)((APM_RC.InputCh(0) + APM_RC.InputCh(1)) / 2)) - vx_g_rcin_middle_roll - 1500; // (-vx_g_rcin_middle_roll - 1500) equals -(vx_g_rcin_middle_roll + 1500) => absolut impuls value //long needed //attention uint succeeds over int
  if (vx_rcin_roll >= 0)
  {
    //Serial.println("roll +");
    return ((vx_rcin_roll * 100) / vx_g_rcin_diff_max_roll);  //first times 100 to avoid floatingpoint numbers, or at least reduce the approximation error
  }
  else
  {
    //Serial.println("roll -");
    return ((vx_rcin_roll * 100) / vx_g_rcin_diff_min_roll);
  }
}



//returns relative stick position from -100 to 100 for nick
int vx_g_rcin_nick()
{
  long vx_rcin_nick = (((int)(APM_RC.InputCh(1)) - (int)(APM_RC.InputCh(0))) / 2) - vx_g_rcin_middle_nick;// cast from unsigned to signed int not needed because value range isn't to big. it's a difference so no problem there, due to conversion

  if (vx_rcin_nick >= 0)
  {
    //Serial.println("nick +");

    return ((vx_rcin_nick * 100) / vx_g_rcin_diff_max_nick);  //long still more efficient than float
  }
  else
  {
    //Serial.println("nick -");
    return ((vx_rcin_nick * 100) / vx_g_rcin_diff_min_nick);
  }
}




void vx_g_setmiddledeltawing()    //TRIM value, not real middle -- trim := offset from middle
{
  vx_g_rcin_middle_roll = (( APM_RC.InputCh(0) + APM_RC.InputCh(1)) / 2) - 1500;
  vx_g_rcin_middle_nick = ((int)(APM_RC.InputCh(1)) - (int)(APM_RC.InputCh(0))) / 2;
  vx_g_rcin_middle_0 = APM_RC.InputCh(0);
  vx_g_rcin_middle_1 = APM_RC.InputCh(1);
  Serial3.print("vx_g_rcin_middle_0: ");
  Serial3.println(vx_g_rcin_middle_0);  
  Serial3.println("vx_g_rcin_middle_1: ");
  Serial3.println(vx_g_rcin_middle_1); 

  
}


//calculate difference between middle and full stick position to calibrate actual stick travel for percentual stick travel
void vx_g_setdiffdeltawing()   
{
      vx_g_rcin_diff_min_roll = vx_g_rcin_middle_roll + 1500 - vx_g_rcin_min_roll;
      vx_g_rcin_diff_max_roll = vx_g_rcin_max_roll - vx_g_rcin_middle_roll - 1500;
      vx_g_rcin_diff_min_nick = vx_g_rcin_middle_nick + 1500 - vx_g_rcin_min_nick;
      vx_g_rcin_diff_max_nick = vx_g_rcin_max_nick - vx_g_rcin_middle_nick - 1500;      
}
